// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import MainGame from "../game/MainGame";
import Order from "../order/Order";
import config from "../config/config";
import AudioManage from "../manage/AudioManage";
import Waiter from "../waiter/Waiter";
import func from "../config/func";
import Guide from "../guide/Guide";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Cooking extends cc.Component {

    @property({ type: cc.Sprite, tooltip: "茶壶" })
    m_teapot: cc.Sprite = null;
    @property({ type: cc.Sprite, tooltip: "制作过程中" })
    m_sample: cc.Sprite = null;
    @property({ type: cc.Sprite, tooltip: "制作的食物" })
    m_food: cc.Sprite = null;
    @property({ type: cc.Sprite, tooltip: "制作进度" })
    m_finish: cc.Sprite = null;
    @property({ type: cc.Sprite, tooltip: "制作完成" })
    m_complete: cc.Sprite = null;
    @property({ type: cc.Node, tooltip: "服务生" })
    m_waiter: cc.Node = null;
    @property(cc.Sprite)
    m_money: cc.Sprite = null;
    @property(cc.Label)
    m_moneyLabel: cc.Label = null;

    n_cooking: boolean = false;
    n_startCut: boolean = false;
    n_accelerate: boolean = false;
    n_cookSpeed: number = 0;
    n_accelerateSpeed: number = 0;
    n_mainGame: MainGame = null;
    n_order: cc.Node = null;
    n_cookInfo: cook = null;
    // n_cook: cc.Animation = null;
    n_waiter: Waiter = null;
    n_teapot: cc.Tween = null;

    start() {
        this.n_mainGame = this.node.parent.getComponent(MainGame);
        this.n_waiter = this.m_waiter.getComponent(Waiter);
        this.n_order = cc.find('Canvas/game/orderNode');
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.m_complete.node.on(cc.Node.EventType.TOUCH_END, this.touchFood, this);
        cc.tween(this.m_complete.node)
            .repeatForever(
                cc.tween()
                    .to(1, { scale: 0.7 })
                    .to(1, { scale: 1.2 })
            )
            .start();
        this.n_teapot = cc.tween(this.m_teapot.node)
            .repeatForever(
                cc.tween()
                    .to(1, { scaleY: 0.7 })
                    .to(1, { scaleY: 1 })
            )
    }
    /**
     * 初始化设置
     */
    setSampleAndFoodActive() {
        this.n_cooking = false;
        this.n_startCut = false;
        this.m_sample.node.active = false;
        this.m_complete.node.parent.active = false;
    }

    init(cookInfo: cook, guide: boolean = false) {
        if (guide && config.guide && !config.guideFood) {
            let pos = cc.v2(this.node.x + this.m_teapot.node.x, this.node.y + this.m_teapot.node.y + this.m_teapot.node.height / 2);
            Guide.instance.showGuide(this.m_teapot.node.width, this.m_teapot.node.height, pos, true);
        }
        this.n_cookInfo = cookInfo;
        this.n_cooking = true;
        this.n_startCut = true;
        this.m_finish.fillRange = 0;
        this.m_sample.node.active = true;
        this.m_complete.node.parent.active = false;
        this.n_cookSpeed = config.basicsTime / (config.orderArr[0].cookingTime / (1 + config.cook_2.additionTime / 100)) * config.addSpeed;
        this.n_accelerateSpeed = this.n_cookSpeed * 3 * config.addSpeed;
        // this.n_mainGame.startCooking();
        this.loadPlist();
        this.n_teapot.start();
    }
    /**
    * 加载图集
     */
    async loadPlist() {
        let plist: cc.SpriteAtlas = null;
        if (config.spriteAtlas[config.spriteAtlasPath.food]) {
            plist = config.spriteAtlas[config.spriteAtlasPath.food];
        } else {
            plist = await func.loadResouce(config.spriteAtlasPath.food, cc.SpriteAtlas) as cc.SpriteAtlas;
            config.spriteAtlas[config.spriteAtlasPath.food] = plist;
        }
        let key = `vegetableType_${config.orderArr[0].id}`;
        this.m_food.spriteFrame = plist.getSpriteFrame(key);
        this.m_complete.spriteFrame = this.m_food.spriteFrame;
    }
    touchStart(e: any) {
        if (e.getLocation().length > 1 || !this.n_startCut) return;
        this.n_accelerate = true;
    }
    touchCancel(e: any) {
        if (!this.n_startCut) return;
        this.n_accelerate = false;
    }
    touchEnd(e: any) {
        if (e.getLocation().length > 1 || !this.n_startCut) return;
        this.n_accelerate = false;
    }

    /**
     * 点击食物完成订单
     */
    touchFood() {
        if (this.n_waiter.n_move) return;
        this.n_waiter.moveWaiter(this.m_food.spriteFrame);
        AudioManage.audioManage.playEffectMusic(config.soundPath.click);
        this.n_cooking = false;
        this.m_complete.node.parent.active = false;
        let reward = config.orderArr[0].reward + this.n_cookInfo.additionMoney;
        this.n_mainGame.n_orderReward += reward;
        this.n_order.getComponent(Order).moveOrderNode(config.orderArr[0], this.n_order.children[0]);
        this.n_mainGame.completeOrder(config.orderArr[0].addFinsh);
        config.orderArr.splice(0, 1);
        if (config.orderArr.length >= 3) {
            this.n_order.getComponent(Order).creatOrder();
        }
        this.n_mainGame.checkCookState(2);
        this.m_money.node.active = true;
        this.m_moneyLabel.string = `+${reward}`;
        cc.tween(this.m_money.node)
            .delay(1)
            .to(1, { y: this.m_money.node.y + 100, opacity: 0 })
            .call(() => {
                this.m_money.node.y -= 100;
                this.m_money.node.opacity = 255;
                this.m_money.node.active = false;
            })
            .start();
    }

    update() {
        if (this.n_startCut) {
            if (this.n_accelerate) {
                this.m_finish.fillRange += this.n_accelerateSpeed;
            } else {
                this.m_finish.fillRange += this.n_cookSpeed;
            }
            if (this.n_accelerate) {
                if(config.gameType == "wx"){
                    window["wx"].vibrateShort({
                        success() {
                            
                        }
                    })
                }else if(config.gameType == "QQ"){
                    window["qq"].vibrateShort({
                        success() {
                            
                        }
                    })
                }
                
            }
            if (this.m_finish.fillRange >= 1) {
                if (config.guide && !config.guideFood) {
                    config.guideFood = true;
                    let pos = cc.v2(this.node.x + this.m_complete.node.parent.x, this.node.y + this.m_complete.node.parent.y);
                    Guide.instance.showGuide(this.m_complete.node.parent.width, this.m_complete.node.parent.height, pos, true);
                }
                this.m_finish.fillRange = 1;
                this.n_startCut = false;
                this.n_accelerate = false;
                this.m_sample.node.active = false;
                this.m_complete.node.parent.active = true;
                this.n_teapot.stop();
                this.m_teapot.node.scale = 1;
                // this.n_cook.stop();
            }
        }
    }
}
